//index.js 
import {
  getInit,
  textToAddressInfo,
} from '../../utils/networkRequest.js'
import {
  errorMsg
} from '../../utils/tool.js'
//获取应用实例
const app = getApp()

Page({
  data: {
    dataInfo: {},
    showPop: false,

    shouName: "",
    shouPhone: "",
    shouShenShiQu: "",
    shouAddress: "",
  },

  onLoad: function(options) {
    debugger;
    if (options.opInfo) {
      let opInfo = JSON.parse(options.opInfo);
      if (opInfo.action == "view_order") {
        wx.navigateTo({
          url: unescape(opInfo.url),
        })
      }
    }

    
  },

  onShow: function() {
    let self = this;
    getInit((r) => {
      if (r.code == 0) {
        console.log(r)
        self.setData({
          dataInfo: r.data
        })
        wx.setStorageSync('sender_info', JSON.stringify(r.data.sender_info))
      } else {
        errorMsg(r.message)
      }
    })

    wx.getClipboardData({
      success: function(res) {
        let copyText = wx.getStorageSync('copyText')
        if (copyText != res.data) {
          textToAddressInfo(res.data, (r) => {
            if (r.code == 0) {
              wx.setStorageSync('copyText', res.data)
              if (r.data.name || r.data.mobile || r.data.p_c_b || r.data.address) {
                self.setData({
                  showPop: true,
                  shouName: r.data.name,
                  shouPhone: r.data.mobile,
                  shouShenShiQu: r.data.p_c_b,
                  shouAddress: r.data.address
                })
              }
            } else {
              r.message && errorMsg(r.message)
            }
          })
        }

      }
    })
  },
  //获取收件人姓名
  getShouName: function(e) {
    this.setData({
      shouName: e.detail.value
    })
  },
  //获取收件人电话
  getShouPhone: function(e) {
    this.setData({
      shouPhone: e.detail.value
    })
  },
  //获取收件人所在省市区
  getShouShenShiQu: function(e) {
    this.setData({
      shouShenShiQu: e.detail.value
    })
  },
  //获取收件人详细地址
  getShouAddress: function(e) {
    this.setData({
      shouAddress: e.detail.value
    })
  },
  //跳转到修改用户信息
  goChangeUserInfo: function() {
    wx.navigateTo({
      url: '../changeuserinfo/changeuserinfo'
    })
  },
  //事件处理函数
  goMyOrderList: function() {
    wx.navigateTo({
      url: '../myorderbox/myorderlist/myorderlist'
    })
  },
  //前往开单
  goOpenBill: function() {
    let self = this;
    let {
      shouName,
      shouPhone,
      shouShenShiQu,
      shouAddress
    } = this.data;


    let shouAddressInfo = {
      name: shouName,
      phone: shouPhone,
      shenShiQu: shouShenShiQu,
      address: shouAddress
    }
    if (shouName || shouPhone || shouShenShiQu || shouAddress) {
      self.setData({
        showPop: false,
        shouName: "",
        shouPhone: "",
        shouShenShiQu: "",
        shouAddress: ""
      })
      wx.navigateTo({
        url: '../openbillbox/selectseller/selectseller?openType=kaidan&shouAddressInfo=' + JSON.stringify(shouAddressInfo) + "&iscopy=true"
      })
    } else {
      wx.navigateTo({
        url: '../openbillbox/selectseller/selectseller?openType=kaidan'
      })
    }


  },
  goPartnerList: function() {
    wx.navigateTo({
      url: '../invitationbox/partnerlist/partnerlist?team_id=' + this.data.dataInfo.team_id
    })
  },
  //跳转到我的团队
  goTeamList: function(e) {
    let teamId = e.currentTarget.dataset.teamid;
    wx.navigateTo({
      url: '../teambox/teamlist/teamlist?team_id=' + teamId
    })
  },
  //跳转到帐目统计
  goReportForm: function() {
    wx.navigateTo({
      url: '../reportformbox/reportformindex/reportformindex'
    })
  },
  //跳转到快递公司列表
  goExpressList: function() {
    wx.navigateTo({
      url: '../expressbox/expresslist/expresslist'
    })
  },
  goPrinter: function() {
    if (this.data.dataInfo.has_printer) {
      wx.navigateTo({
        url: '../printerbox/printerlist/printerlist',
      })
    } else {
      wx.navigateTo({
        url: '../printerbox/noprinter/noprinter',
      })
    }

  },
  goZhiNan: function() {
    wx.navigateTo({
      url: '../zhinanbox/zhinanlist/zhinanlist',
    })
  },
  //关闭弹窗
  popClose: function() {
    this.setData({
      showPop: false
    })
  },
  gotoMaiJia: function() {
    wx.reLaunch({
      url: '../about/about?goto=maijia',
    })
  },
  //跳转至售后列表
  goAftersale: function() {
    wx.navigateTo({
      url: '../aftersalebox/aftersalelist/aftersalelist',
    })
  },
  //功能未完成
  onShowTips: function() {
    errorMsg("拼命加班中...")
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    debugger;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
      return {
        title: '',
        path: "pages/openbillbox/addgoods/addgoods?shareid=" + this.data.dataInfo.leader_id,
        imageUrl: "../../assets/images/kaidanshare.png",

      }
    }else{
      return {
        title: '',
        path: "pages/about/about"

      }
    }
   
  },
})